package com.translate.web.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import com.common.util.CopyEntity;
import com.common.util.TheMagicSkillOfBeingInvisible;
import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.UserMapper;
import com.translate.entity.User;
import com.page.PageResponseData;
import com.param.contract.IPage;
import com.translate.web.service.ICommunicationUserService;
import com.basic.service.impl.BaseServiceImpl;
import com.translate.vo.contract.IContactsHumanVO;

/**
 * 通讯录服务
 * @author 111
 */
@Service
public class CommunicationUserService extends BaseServiceImpl<User> implements ICommunicationUserService {

	private RedisTemplate<String, PageResponseData<User>> redisTemplate;
	
	public CommunicationUserService(UserMapper mapper) {
		super(mapper);
	}
	
	

//	@Override
//	public <S extends IContactsHumanVO> List<S> topLeavel(IPage page, Class<S> target) {
//		
//		List<User> list = this.usersToCache(page);
//		
//		return datatoS(list, target);
//	}
	
	@Override
	protected User getEntityInstance() {
		return new User();
	}

	protected String getTableNameNoFrontSuffix() {
		return "_user";
	}

	@Override
	protected Class<User> getEntityClass() {
		// TODO Auto-generated method stub
		return User.class;
	}



	protected <VO> void editVO(VO vo, User t) {

		IContactsHumanVO contactsHumanVO = (IContactsHumanVO)vo;
		
		contactsHumanVO.setFlag(TheMagicSkillOfBeingInvisible.GetEncodeUserID(t.getId()));
	}
	
	
	protected <S extends IContactsHumanVO> List<S> datatoS(List<User> users, Class<S> target) {
		
		List<S> retData = new ArrayList<>();
		
		S vo;
		
		for (User user : users) {
			
			vo = CopyEntity.copyEntity(user, target);
		
			vo.setFlag(TheMagicSkillOfBeingInvisible.GetEncodeUserID(user.getId()));
			
			retData.add(vo);
		}
		
		return retData;
	}
	
//	protected List<User> usersToCache(IPage page) {
//	
//		String key = RedisConstant.getCommunication(page.getPageNum());
//		
//		ValueOperations<String , List<User>> valueOperations = this.redisTemplate.opsForValue();
//		
//		List<User> users = valueOperations.get(key);
//		
//		if (null != users) {
//			return users;
//		}
//		
//		UserMapper mapper = this.getBaseMapper();
//		
//		users = mapper.topLeavel(page.getStart(), page.getSelectCount());
//		
//		if (null == users) {
//			users = new ArrayList<>(0);
//			
//			valueOperations.set(key, users, 3, TimeUnit.SECONDS);
//			
//			return users;
//		}
//		
//		valueOperations.set(key, users, 6, TimeUnit.SECONDS);
//
//		return users;
//	}
	
	@Autowired
	public void setRedisTemplate(RedisTemplate<String, PageResponseData<User>> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}
	
	/**
	 * 部门通讯人员缓存
	 * @param page
	 * @return
	 */
	protected PageResponseData<User> departmentCommunicationsUsersToCache(IPage page) {
		
		String key = RedisConstant.getDepartment(page.getPageNum());
		
		ValueOperations<String , PageResponseData<User>> valueOperations = this.redisTemplate.opsForValue();
		
		PageResponseData<User> pageResponseData = valueOperations.get(key);
		
		if (null != pageResponseData) {
			return pageResponseData;
		}
		
		UserMapper mapper = this.getBaseMapper();
		
		Integer count = mapper.countByDepartmentCommunications(page);
		
		if (null == count) {
			pageResponseData = new PageResponseData<>();
			
			valueOperations.set(key, pageResponseData, 3, TimeUnit.SECONDS);
			
			return pageResponseData;
		}
		
		List<User> users = mapper.departmentCommunications(page);
		
		pageResponseData =new PageResponseData<>(count, users);
		
		valueOperations.set(key, pageResponseData, 6, TimeUnit.SECONDS);

		return pageResponseData;
		
	}

	/**
	 * 获取部门通讯录
	 */
	@Override
	public <S extends IContactsHumanVO> PageResponseData<S> departmentCommunications(IPage page, Class<S> target) {
		PageResponseData<User> pageResponseData = this.departmentCommunicationsUsersToCache(page);
		
		List<S> vos =datatoS(pageResponseData.getData(), target);
		
		return new PageResponseData<>(pageResponseData.getTotal(), vos);
	}


//	@Override
//	public <S extends IContactsHumanVO> List<S> departmentCommunicationsBySearch(
//			ICommunicationsManBySearchParam bySearchParam, Class<S> target) {
//		// TODO Auto-generated method stub
//		return null;
//	}

}
